package com.decheng.servlet;

import com.decheng.dto.PaginationDTO;
import com.decheng.pojo.Book;
import com.decheng.service.BookService;
import com.decheng.service.impl.BookServiceImpl;
import com.decheng.servlet.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 */
public class BookManageServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();

    /**
     * 跳转到图书添加页面或更新图书页面
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void toAddBookPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        // 如果有id，则查询书籍信息，放到请求域中
        if (!StringUtils.isEmptyOrWhitespace(id)) {
            // 查询书籍信息
            Book book = new Book();
            book.setBookId(Integer.valueOf(id));
            Book queryBook = bookService.getBook(book);
            req.setAttribute("book", queryBook);
        }
        engine.processTemplate("manager/book_edit", req, resp);
    }


    /**
     * 跳转到图书添加页面
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void toBookManagerPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 1. 获取请求参数
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        // 2. 查询数据
        PaginationDTO<Book> bp = bookService.showBookList(currentPage, pageSize);
        req.setAttribute("data", bp);
        // 3. 渲染视图
        engine.processTemplate("manager/book_manager", req, resp);
    }

    /**
     * 添加图书或更新图书
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void addBook(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bookId = req.getParameter("bookId");
        Book book = new Book();
        Map<String, String[]> parameterMap = req.getParameterMap();
        try {
            BeanUtils.populate(book, parameterMap);
            if (!StringUtils.isEmptyOrWhitespace(bookId)) {
                // 执行更新
                bookService.updateBook(book);
            } else {
                // 给图书设置固定图片
                book.setImgPath("jiaofu.jpg");
                // 执行添加
                bookService.addBook(book);
            }
            // 重定向到图书列表页
            resp.sendRedirect(req.getContextPath() + "/bookManager?method=toBookManagerPage");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除图书
     */
    public void removeBook(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 1. 获取图书 id
        Integer id = Integer.valueOf(req.getParameter("id"));
        try {
            // 2. 执行删除业务
            bookService.removeBook(id);
            // 2. 重定向到图书管理页面
            resp.sendRedirect(req.getContextPath() + "/bookManager?method=toBookManagerPage");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
